const args = require('minimist')(process.argv.slice(2));
const path = require('path')

const target = args._[0] || 'reactivity'
// 格式
const format = args.f || 'global'
// 入口
const entry = path.resolve(__dirname, `../packages/${target}/src/index.ts`)
// 名字
const globalName = require(path.resolve(__dirname, `../packages/${target}/package.json`)).buildOptions?.name

console.log('args---->', args);
console.log('format---->', format, process.argv.slice(2));


// iife 自执行函数
// cjs commonjs 规范
// esm es6Module 格式

// 输出格式
const outputFormat = format.startsWith('global') ? 'iife' : format === 'cjs' ? 'cjs' : 'esm'
// 打包后结果路径
const outfile = path.resolve(__dirname, `../packages/${target}/dist/${target}.${format}.js`)

const {build} = require('esbuild')

build({
    entryPoints: [entry],
    outfile,
    bundle: true,
    sourcemap: true,
    format: outputFormat,
    globalName,
    platform: format === 'cjs' ? 'node' : 'browser',
    watch: {
        onRebuild(error) {
            if(!error) console.log(`rebuilt~~~~`);
        }
    }

}).then(()=>{
    console.log('watching~~~~');
})

console.log(globalName, outfile, entry, outputFormat, target);